var Crawler = require('crawler');
module.exports = function (obj) {
    return new Promise((resolve, reject) => {
        var crawler;
        if (obj.type) {
            crawler = new Crawler({
                maxConnections: obj.maxConnections ? obj.maxConnections : 10,
                encoding: null,
                // jQuery:false,// set false to suppress warning message.
                callback: function (err, res, $) {
                    // fs.createWriteStream('./1.js').write(res.body);
                    resolve({err, res, $})
                    // if(obj.func){
                    //     obj.func(err, result, $)
                    // }
                    // $('li').each(function(index, li) {
                    //     console.log(index + ' :');
                    //     console.log('time:' + $(li).children(0).text());
                    //     console.log('url:' + result.uri + $(li).children(1).attr('href'));
                    //     console.log('title:' + $(li).children(1).text());
                    // });
                }
            });
        } else {
            crawler = new Crawler({
                maxConnections: obj.maxConnections ? obj.maxConnections : 10,
                callback: function (err, res, $) {
                    resolve({err, res, $})
                }
            });
        }
        crawler.queue(obj.url ? obj.url : 'https://www.baidu.com');
    })
}
